package com.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.db.InfoProduct;
import com.db.MysqlConnect;

public class Transition {
	
	public String convertAlias(String value){
		try {
			if(value==null || value.length()==0) return null;
			
			String result=value.trim();	
			
			VietDecode vd= new VietDecode();			
			result = vd.convert(value);
			
			result = result.replace(" ", "-");
			return result.toLowerCase();
			
		} catch (Exception e) {
			return e.toString();
		}
	}
	
	public String keywordAnalyse(InfoProduct inf){
		
		try {
			
			String name = inf.Name;
			String nameConvert = null;
			String keyword = null;
			String result=null;			
						
			if(name==null || name.length()==0) return "";
			
			VietDecode vd = new VietDecode();
			nameConvert = vd.convert(name).toLowerCase();
			
			keyword = getKeywordCategory(inf.CatId);
			
			result = nameConvert +"," +keyword;
			
			return result;
			
		} catch (Exception e) {
			return "";
		}
		
	}
	
	
	public String getKeywordCategory(String cat){
		
		try {
			String keyword=null;
			
			if(cat==null || cat.length()==0) return "";
			
			PreparedStatement pstmt = null;
		    Connection conn  = null;		    
			MysqlConnect mysql = new MysqlConnect();
			conn = mysql.getConnection();
			
			String vc_sql="SELECT keyword FROM  `product_cat` WHERE `id`=?";
		    pstmt = conn.prepareStatement(vc_sql);
		    pstmt.setString(1, cat);
		    ResultSet rs = pstmt.executeQuery();
		    
			while (rs.next())
				keyword= rs.getString("keyword");
			
			return keyword;
			
		} catch (Exception e) {
			return e.toString();
		}
	}
	
	public String getCity(String value){
		try {
			String id="0";
			if(value==null || value.length()==0) return "";
			
			PreparedStatement pstmt = null;
		    Connection conn  = null;		    
			MysqlConnect mysql = new MysqlConnect();
			conn = mysql.getConnection();
			//System.out.println("--"+value);
			
			String vc_sql="SELECT id FROM  `city` WHERE `code`=?";
		    pstmt = conn.prepareStatement(vc_sql);
		    pstmt.setString(1, value);
		    ResultSet rs = pstmt.executeQuery();
		    
			while (rs.next())				
				id= rs.getString("id");
			
			//System.out.println("getCity id: "+rs.getInt("id"));
			//System.out.println("getCity id: "+id);
			
			return id;
		} catch (Exception e) {
			System.out.println(e.toString());
			return "0";
		}
	}
	
	public String getCatId(String value){
		try {
			String id="";
			if(value==null || value.length()==0) return "";
			
			PreparedStatement pstmt = null;
		    Connection conn  = null;		    
			MysqlConnect mysql = new MysqlConnect();
			conn = mysql.getConnection();
			//System.out.println("--"+value);
			
			String vc_sql="SELECT id FROM  `product_cat` WHERE `id_tkm`=?";
		    pstmt = conn.prepareStatement(vc_sql);
		    pstmt.setString(1, value);
		    ResultSet rs = pstmt.executeQuery();
		    
			while (rs.next())				
				id= rs.getString("id");
			
			return id;
		} catch (Exception e) {
			System.out.println(e.toString());
			return "";
		}
	}
	
	
	public void displayInfoProduct(InfoProduct inf){
		System.out.println("Name: "+inf.Name);
		System.out.println("Code: "+inf.Code);
		System.out.println("Type: "+inf.Type);
		System.out.println("Conent: "+inf.Content);
		System.out.println("Price: "+inf.Price);
		System.out.println("Price Sale: "+inf.PriceSale);
		System.out.println("Date begin: "+inf.BeginDateSale);
		System.out.println("Date end: "+inf.EndDateSale);
		System.out.println("YM: "+inf.Ym);
		System.out.println("Email: "+inf.Email);
		System.out.println("People: "+inf.People);
		System.out.println("Mobile: "+inf.Mobile);
		System.out.println("Phone: "+inf.Phone);
		System.out.println("Address: "+inf.Address);
		System.out.println("Img: "+inf.Img);
		System.out.println("Summary: "+inf.Summary);
		System.out.println("Domain: "+inf.City);
	}
	
	public static void main(String[] args) {
		Transition t = new Transition();
		//System.out.println(t.getCity("hanoi"));
		System.out.println(t.getCatId("295"));
	}
}
